
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import jxl.*;
import jxl.read.biff.BiffException;

public class MatrixExcelLoader implements IMatrixLoader {
	
	protected String filename = null;
	protected ArrayList<String> headers = null;
	
	public MatrixExcelLoader(String name)
	{
		filename = new String(name);
		headers = new ArrayList<String>(); 
	}

	public Matrix load() {
		Matrix matrix = null;
		try {
			Workbook workbook = Workbook.getWorkbook(new File(filename));
			
			Sheet sheet = workbook.getSheet(0);
			
			int columnSize = sheet.getColumns();
			int rowSize = sheet.getRows();
			
			matrix = new Matrix(rowSize, columnSize);
			
			for(int i = 0; i < columnSize;i++)
				headers.add(sheet.getCell(i, 0).getContents());
			
			for(int row = 1; row < rowSize; row++)
			{
				for(int col = 0; col < columnSize; col++)
				{
					String content = sheet.getCell(col, row).getContents();
					if(content.indexOf(',') != -1)
						content = content.replace(',', '.');
					matrix.set(row, col, new Double(content));
				}
			}		
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return matrix;
	}

	
	public ArrayList<String> getHeaders() {
		// TODO Auto-generated method stub
		return headers;
	}
	

}
